package com.rjpacket.supernews.app.fragments;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.rjpacket.supernews.app.NewsDetailActivity;
import com.rjpacket.supernews.app.R;
import com.rjpacket.supernews.app.adapters.NewsFragmentAdapter;
import com.rjpacket.supernews.app.databases.DBHelper;
import com.rjpacket.supernews.app.models.News;
import com.rjpacket.supernews.app.models.NewsDetail;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

public class SaveArticleFragment extends Fragment implements AdapterView.OnItemClickListener {

    private List<News> data;

    public SaveArticleFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_save_article, container, false);

        ListView articleList = (ListView) view.findViewById(R.id.save_article_listView);

        data = new LinkedList<News>();

        //TODO 查询数据库
        DBHelper dbHelper = new DBHelper(getActivity());

        SQLiteDatabase database = dbHelper.getWritableDatabase();

        Cursor favorite = database.query("myFavorite", null, null, null, null, null, null);

        if (favorite != null) {
            while (favorite.moveToNext()) {
                String str = favorite.getString(favorite.getColumnIndex("jsonNews"));
                String nid = favorite.getString(favorite.getColumnIndex("position"));
                if (str != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(str);

                        News news = new News();
                        news.parseJSON(jsonObject);

                        List<News> newsList = news.getList();

                        News news1 = null;
                        for (News n : newsList) {
                            String nid1 = n.getNid();
                            if (nid1.equals(nid)) {
                                // 获得了目的对象
                                news1 = n;
                            }
                        }

                        if (news1 != null) {
                            Cursor detailCursor = database.query("myDetails", null, "nid = ?", new String[]{nid}, null, null, null);

                            if (detailCursor != null && detailCursor.moveToNext()) {
                                String jsonDetail = detailCursor.getString(detailCursor.getColumnIndex("jsonDetail"));

                                JSONObject jsonObject1 = new JSONObject(jsonDetail);

                                NewsDetail detail = new NewsDetail();

                                detail.parseJSON(jsonObject1);

                                news1.setNewsDetail(detail);
                            }

                            data.add(0, news1);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        NewsFragmentAdapter adapter = new NewsFragmentAdapter(getActivity(), data);

        articleList.setAdapter(adapter);
        articleList.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
        intent.putExtra("news", data.get(position));
        startActivity(intent);
    }
}
